package com.mspbots.core.ticketai;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.ticketai.model.BotTicketCache;
import com.mspbots.core.ticketai.model.Ticket;
import com.mspbots.core.ticketai.service.TicketService;
import lombok.Data;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.concurrent.Callable;

/**
 * com.mspbots.core.ticketai.TicketController
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/8/8
 */
@RestController
@RequestMapping("tickets")
public class TicketController extends AbstractBaseController<Ticket, TicketService> {

    @GetMapping(path = "/list-for-rule")
    public Callable selectForRule(Page<Ticket> page, Ticket queryParams) {
        return () -> service.listForRule(page, queryParams);
    }

    @GetMapping(path = "/selectbycwId")
    public Callable selectByCwId(Ticket queryParams) {
        return () -> service.selectByCwId(queryParams);
    }

    @GetMapping(path = "/detail")
    public Callable detail(Ticket queryParams) {
        QueryWrapper<Ticket> queryWrapper = new QueryWrapper<>(queryParams);
        if (ObjectUtils.isNotEmpty(queryParams.getCwId())) {
            queryWrapper.eq("cw_id", queryParams.getCwId());
        }
        queryWrapper.eq("tenant_id", queryParams.getTenantId());
        return () -> service.getOne(queryWrapper);

    }

    @GetMapping(path = "next")
    public Callable nextTicket(TicketQueryParams params) {
        Assert.notNull(params.getTenantId(), "Property or field 'tenantId' cannot be found on null");
        Assert.notNull(params.getBoardId(), "Property or field 'boardId' cannot be found on null");
        return () -> this.service.nextList(params);
    }

    @GetMapping("list-for-db")
    public Callable getTicketList(TicketQueryParams params) {
        Assert.notNull(params.getTenantId(), () -> "Search param [tenant id] not null.");
        return () -> this.service.getTicketList(params);
    }

    @GetMapping("list-assigned-ticket")
    public Callable getAssignedTicketList(Long tenantId, String ticketIds) {
        return () -> this.service.getAssignedTicketList(tenantId, ticketIds);
    }

    @DeleteMapping("delete-batch")
    public Callable<Integer> deleteBatch(TicketQueryParams params) {
        Assert.notNull(params.getTenantId(), () -> "Search param [tenant id] not null.");
        return () -> this.service.deleteBatch(params);
    }

    @GetMapping("page-reports")
    public Callable selectPageReports(Page<Ticket> page, Ticket queryParams) {
        QueryWrapper<Ticket> queryWrapper = new QueryWrapper<>(queryParams);
        return () -> {
            Page<Ticket> ticketPage = service.page(page, queryWrapper);
            if (!CollectionUtils.isEmpty(ticketPage.getRecords())) {
                ticketPage.getRecords().stream().forEach(ticket -> {
                    ticket.setExtend(null);
                    ticket.setAuditor(null);
                    ticket.setTeamRoles(null);
                });
            }
            return ticketPage;
        };
    }

    @GetMapping("findNextTicketRate")
    public Callable findNextTicketRate(Ticket queryParams) {
        return () -> this.service.findNextTicketRate(queryParams);
    }

    @PostMapping("setCache")
    public Callable setCache(@RequestBody BotTicketCache queryParams) {
        return () -> this.service.setCache(queryParams);
    }

    @GetMapping("removeCache/{userId}/{scriptId}")
    public Callable removeCache(@PathVariable("userId") Long userId, @PathVariable("scriptId") Long scriptId) {
        BotTicketCache botTicketCache = new BotTicketCache();
        botTicketCache.setUserId(userId);
        botTicketCache.setScriptId(scriptId);
        return () -> this.service.removeCache(botTicketCache);
    }

    @GetMapping("getCache/{userId}/{scriptId}")
    public Callable getCache(@PathVariable("userId") Long userId, @PathVariable("scriptId") Long scriptId) {
        BotTicketCache botTicketCache = new BotTicketCache();
        botTicketCache.setUserId(userId);
        botTicketCache.setScriptId(scriptId);
        return () -> this.service.getCache(botTicketCache);
    }

    @Data
    public static class TicketQueryParams {
        private Long id;
        private Long tenantId;
        private Long cwId;
        private Long boardId;
        private String status;
        private Long owner;
        private LocalDateTime createdTime;
        private LocalDateTime updatedTime;
        private LocalDateTime syncTime;
        private Boolean allAssigned;
        private String boardIds;
        private String statusIds;
        private String boardAssignedIds;
        private String statusAssignedIds;
        private String resources;
        private String rejectTicketIds;
        private String type;
    }

}
